package com.project.web.controller.user;

import com.project.common.utils.ShiroUtils;
import com.project.system.domain.SysUser;
import com.project.system.service.ISysUserService;
import com.project.user.domain.UserAddress;
import com.project.user.domain.UserBrowseRecord;
import com.project.user.service.IUserAddressService;
import com.project.user.service.IUserBrowseRecordService;
import com.project.web.controller.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 用户中心 信息操作处理
 *
 * @author administrator
 */
@Controller
@RequestMapping("/user")
public class UserCenterController extends BaseController {
    private String prefix = "normal";

    @Autowired
    private IUserBrowseRecordService browseRecordService;
    @Autowired
    private IUserAddressService userAddressService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 跳转个人中心
     *
     * @param menuType
     * @param status
     * @param modelMap
     * @return
     */
    @GetMapping("/{menuType}")
    public String userCenter(@PathVariable("menuType") String menuType, String status, ModelMap modelMap) {
        modelMap.put("topTitle", "个人中心");
        modelMap.put("menuType", menuType);
        if ("info".equals(menuType)) {
            UserBrowseRecord record = new UserBrowseRecord();
            record.setUserId(this.getUserId());
            record.getParams().put("limitNum", "20");
            modelMap.put("browseRecordList", browseRecordService.selectUserBrowseRecordList(record));
            return prefix + "/user_center_info";
        } else if ("order".equals(menuType)) {
            modelMap.put("status", status == null ? "" : status);
            return prefix + "/user_order";
        } else if ("prefer".equals(menuType)) {
            return prefix + "/user_center_prefer";
        } else if ("addr".equals(menuType)) {
            List<UserAddress> addrList = userAddressService.selectUserAddressList(new UserAddress(this.getUserId()));
            modelMap.put("addrList", addrList);
            return prefix + "/user_addr";
        } else if ("remark".equals(menuType)) {
            return prefix + "/user_remark";
        } else if ("browse".equals(menuType)) {
            return prefix + "/user_browse_record";
        }

        return prefix + "/index";
    }

    /**
     * 修改个人信息
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/updateInfo")
    public String updateInfo(SysUser user, HttpServletRequest request) {
        user.setUserId(this.getUserId());
        sysUserService.updateUser(user);
        ShiroUtils.setUser(sysUserService.selectUserById(this.getUserId()), request.getSession());
        return "redirect:/user/info";
    }
}
